<template>
<!-- 全部课程 -->
  <main role="main">
    <div class="header-nav">
      <div class="clearfix">
        <div class="container">
          <div class="row">
            <div class="col-12">
              <a @click="onClickLevel1('00000000')" id="category-00000000" href="javascript:;" class="cur">全部</a>
              <a v-for="o in level1" @click="onClickLevel1(o.id)" :id="'category-' + o.id" href="javascript:;">{{o.name}}</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="skill clearfix">
      <div class="container">
        <div class="row">
          <div class="col-12">
            <a @click="onClickLevel2('11111111')" id="category-11111111" href="javascript:;" class="on">不限</a>
            <a v-for="o in level2" @click="onClickLevel2(o.id)" :id="'category-' + o.id" href="javascript:;">{{o.name}}</a>

            <div style="clear:both"></div>
          </div>
        </div>
      </div>
    </div>

    <div class="album py-5 bg-light">
      <div class="container">
        <div class="row">
          <div v-for="c in courses" :key="c.id" class="col-md-4">
            <the-course :course="c"></the-course>
          </div>
          <div>
            <h3 v-show="courses.length === 0">暂未上架课程</h3>
          </div>
        </div>
        <pagination ref="paginationRef" :list="listCourse"></pagination>
      </div>
    </div>
  </main>
</template>

<script>
import {ref, onMounted} from "vue";
import axios from 'axios';
import TheCourse from "@/components/the-course";
import pagination from '@/components/pagination'

export default {
  name: "list",
  components: {TheCourse, pagination},
  setup() {
    const courses = ref([]);
    const paginationRef = ref();
    // 一级分类
    const level1 = ref([]);
    // 二级分类
    const level2 = ref([]);
    const categorys = ref([]); // 所有分类
    const level1Id = ref(''); // 点击的一级分类
    const level2Id = ref(''); // 点击的二级分类

    /**
     * 全部课程
     */
    const listCourse = (page) => {
      axios.post(process.env.VUE_APP_SERVER + "/business/web/course/list",{
        page: page,
        size: paginationRef.value.size,
        categoryId: level2Id.value || level1Id.value || '' // 优先查二级分类 没有 一级分类 没有 查所有课程
      }).then((response) => {
        const resp = response.data;
        if (resp.success) {
          courses.value = resp.content.list;
          paginationRef.value.render(resp.content.page, resp.content.total);
        }
      })
    }

    /**
     * 所有分类
     */
    const allCategory = () => {
      axios.post(process.env.VUE_APP_SERVER + "/business/web/category/all").then((response) => {
        const resp = response.data;
        if (resp.success) {
          categorys.value = resp.content;
          // 分类数据 处理成 树形结构
          level1.value = [];
          for (let i = 0; i < categorys.value.length; i++) {
            let c = categorys.value[i];
            // 如果是一级分类
            if (c.parent === '00000000') {
              level1.value.push(c); // 一级分类
            } else {
              level2.value.push(c); // 二级分类
            }
          }
        }
      })
    }

    /**
     * 点击一级分类
     */
    const onClickLevel1 = (id) => {
      level1Id.value = id; // 保存id
      level2Id.value = null;
      // 点击一级分类时，显示激活状态
      $("#category-" + id).siblings("a").removeClass("cur");
      $("#category-" + id).addClass("cur");

      // 点击一级分类时，二级分类【无限】按钮要设置激活状态
      $("#category-11111111").siblings("a").removeClass("on");
      $("#category-11111111").addClass("on");

      level2.value = [];
      // 如果点击的是全部
      if (id === '00000000') {
        level1Id.value = null;
        for (let i = 0; i < categorys.value.length; i++) {
          let c = categorys.value[i];
          if (c.parent !== id) {
            level2.value.push(c);
          }
        }
        // 重新加载课程列表
        listCourse(1);
      } else {
        // 遍历
        for (let i = 0; i < categorys.value.length; i++) {
          let c = categorys.value[i];
          if (c.parent === id) {
            level2.value.push(c);
          }
        }
        // 重新加载课程列表
        listCourse(1);
      }
    }

    /**
     * 点击二级分类
     */
    const onClickLevel2 = (id) => {
      level2Id.value = id;
      $("#category-" + id).siblings("a").removeClass("on");
      $("#category-" + id).addClass("on");
      if (id === '11111111') {
        level2Id.value = null;
      }
      listCourse(1);
    }

    onMounted(() => {
      paginationRef.value.size = 2;
      allCategory();
      listCourse(1); // 查询第一页
    })

    return {
      courses,
      listCourse,
      paginationRef,
      level1,
      level2,
      onClickLevel1,
      onClickLevel2
    }
  }
}
</script>

<style scoped>
/* 头部 一级分类 */
.header-nav {
  height: auto;
  background: #fff;
  box-shadow: 0 8px 16px 0 rgba(28,31,33,.1);
  padding: 16px 0;
  box-sizing: border-box;
  position: relative;
  z-index: 1;
  /*background-color: #d6e9c6;*/
}
.header-nav>div {
  width: 100%;
  padding-left: 12px;
  box-sizing: border-box;
  margin-left: auto;
  margin-right: auto;
  /*background-color: #B4D5AC;*/
}
.header-nav a {
  float: left;
  font-size: 16px;
  color: #07111b;
  line-height: 50px;
  height: 45px;
  position: relative;
  margin-right: 46px;
  font-weight: 700;
}
.header-nav a:hover {
  color: #c80;
}
.header-nav a.cur {
  color: #c80;
}
.header-nav a.cur:before {
  display: block;
}
.header-nav a:before {
  display: none;
  content: ' ';
  position: absolute;
  bottom: 0;
  background: #c80;
  width: 16px;
  height: 3px;
  left: 50%;
  margin-left: -8px;
}
/* 二级分类 */
.skill {
  width: 100%;
  padding: 24px 0 0;
  position: relative;
  margin: 0 auto;
}
.skill a.on {
  color: #c80;
  background: rgba(204,136,0,.1);
}
.skill a {
  float: left;
  margin-right: 20px;
  padding: 0 12px;
  font-size: 14px;
  color: #4d555d;
  line-height: 32px;
  border-radius: 6px;
  margin-bottom: 12px;
}
.skill a:hover {
  background: #d9dde1;
}
</style>